<?php

class Session extends CI_Controller{
	public function __construct()
	{
		parent::__construct();

		$this->load->database();
		$this->load->helper('url');

		$this->load->library('grocery_CRUD');
	}
	public function _example_output($output = null)
	{
		$this->load->view('master/grid',$output);
	}
	public function login()
	{ 
		//var_dump($this->LoginInfo->IsLoggedIn());
		

		if($this->LoginInfo->IsLoggedIn()){
			//die('REDIR');
			redirect('welcome/greeting');
			exit;
		}
		$data = array('err'=>'');
		if($this->input->post('do_login'))
		{
			$username = $this->input->post('username');
			$password = md5($this->input->post('password'));

			$userInfo = $this->db->where(array('username'=>$username,'userpassword'=>$password))->get('admin')->row_array();
			if($userInfo){
				// USER MUST HAVE SESSION
				$this->LoginInfo->SetUserInfo($userInfo);
				$this->LoginInfo->LandingPage();
				return;
			}else{
				$data['err']= 'Sorry, but you had supplied an invalid Username OR Password';
				//return;
			}
		}
		
		// $output = array('output'=> $this->load->view('_session_login',$data,TRUE));

		$this->load->view('_session_login',$data);
		//die('LOGIN');
	}

	public function logout()
	{
		$this->LoginInfo->UnsetUserInfo();
		$this->LoginInfo->Redirect();
	}
	public function password()
	{
		# code...
	}
	public function guest()
	{
		# code...
		echo 'Welcome Guest';
	}
	public function profile()
	{
		# code...
	}
	public function state()
	{
		echo json_encode( array('status'=> $this->LoginInfo->IsLoggedIn()) );
	}

	public function islogin($echo=FALSE)
	{
		//$this->LoginInfo->IsLoggedIn();
	}
}